<?php

/**
 * This is the model class for table "autori".
 *
 * The followings are the available columns in table 'autori':
 * @property integer $id
 * @property string $nome
 * @property string $biografia
 * @property integer $idBanner
 * @property integer $idLogo
 * @property integer $idx_TipiAutore
 * @property integer $idCategoria
 *
 * The followings are the available model relations:
 * @property Immagine $Banner
 * @property Immagine $Logo
 * @property TipoAutore $TipoAutore
 * @property Categoria $Categoria
 * @property Contatti $Contatti
 * @property Galleria $Gallerie
 */
class Autore extends CActiveRecord
{
        public $bannerFile;
        public $logoFile;
    	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Autore the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'autori';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idBanner, idLogo, idx_TipiAutore, idCategoria', 'numerical', 'integerOnly'=>true),
			array('nome', 'length', 'max'=>1000),
                        array('aforisma', 'length', 'max'=>1000),
			array('biografia', 'length', 'max'=>21844),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, nome, biografia, idBanner, idLogo, idx_TipiAutore, idCategoria', 'safe', 'on'=>'search'),
                        array('bannerFile','file','types' => 'jpg, gif, png, jpeg','allowEmpty'=>true),
                        array('logoFile','file','types' => 'jpg, gif, png, jpeg','allowEmpty'=>true),
                        array('nome','required'),
		);
	}
        public function searchByIdCategoria($idCategoria) {
            $criteria = new CDbCriteria;
            $criteria->compare('idCategoria', $idCategoria);
            return new CActiveDataProvider($this, array(
                        'criteria' => $criteria,
                    ));
        }
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'Banner' => array(self::BELONGS_TO, 'Immagine', 'idBanner'),
			'Logo' => array(self::BELONGS_TO, 'Immagine', 'idLogo'),
			'TipoAutore' => array(self::BELONGS_TO, 'TipoAutore', 'idx_TipiAutore'),
			'Categoria' => array(self::BELONGS_TO, 'Categoria', 'idCategoria'),
			'Contatti' => array(self::HAS_MANY, 'Contatto', 'idAutore'),
			'Gallerie' => array(self::HAS_MANY, 'Galleria', 'idAutore'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nome' => 'Nome',
			'biografia' => 'Biografia',
			'idBanner' => 'Banner',
			'idLogo' => 'Logo',
			'idx_TipiAutore' => 'Tipo Autore',
			'idCategoria' => 'Categoria',
                        'aforisma' => 'Aforisma',
                    
		);
	}
        public function deleteCascade()
        {
            /* @property Immagine $Banner
             * @property Immagine $Logo
             * @property TipoAutore $TipoAutore
             * @property Categoria $Categoria
             * @property Contatti $Contatti
             * @property Galleria $Gallerie*/
            if($this->idBanner > 0)
                $this->Banner->deleteCascade();
            if($this->idLogo > 0)
                $this->Logo->deleteCascade();
            if(isset($this->Contatti))
                foreach($this->Contatti as $contatto)
                    $contatto->deleteByPk($contatto->id);
            if(isset($this->Gallerie))
                foreach($this->Gallerie as $galleria)
                    $galleria->deleteCascade();   
            $pathname = 'images/galleries/';
            $destination = $pathname.$this->nome;
            if(is_dir($destination))
                Yii::app()->fileManager->emptyDir($destination,true);
            $this->deleteByPk($this->id);
        }
        public function completeBannerFile($destination)
        { 
            if(!isset($this->bannerFile))
                return true;
            $idBanner = Immagine::NewImage($destination, $this->bannerFile);
            
            if($idBanner < 0)
                return false;
            $this->idBanner = $idBanner;
            return $this->save();
        }
        public function completeLogoFile($destination)
        { 
            if(!isset($this->logoFile))
                return;
            
            $idLogo = Immagine::NewImage($destination, $this->logoFile);
            
            if($idLogo < 0)
                return false;
            $this->idLogo = $idLogo;
            return $this->save();
        }
        
        public function addContatto($idContatto,$title,$contacts)
        {
            $contatto = new Contatto();
            $contatto->TipoContatto = TipoContatto::model()->findByPk($idContatto);
            $contatto->title = $title;
            $contatto->contacts = $contacts;
            $contatto->idAutore = $model->id;
            if(!$contatto->save())
                return false;
            if(!isset($this->Contatti))
                    $this->Contatti = array();
            $this->Contatti[] = $contatto;
            return $this->save();
        }
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
        
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('nome',$this->nome,true);
		/*$criteria->compare('biografia',$this->biografia,true);
		$criteria->compare('idBanner',$this->idBanner);
		$criteria->compare('idLogo',$this->idLogo);*/
                
                $criteria->compare('TipoAutore.descr',$this->TipoAutore,true);
                $criteria->compare('Categoria.descr',$this->Categoria,true);
                $criteria->join = 'join X_TipiAutore on X_TipiAutore.id = t.idx_TipiAutore join categorie on categorie.id = t.idCategoria';
                $criteria->with = array('TipoAutore','Categoria');
                
		/*
                $criteria->join = 'join categorie on categorie.id = t.idCategoria';
                $criteria->with = array('Categoria');                */
		

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}